
Imports System
Imports System.Collections
Imports System.Data.SqlClient
Imports System.Diagnostics
Imports _2bMatesSite.Common


Namespace _2bMatesSite.BO
	''' <summary>
	''' Summary description for UsersBlocked.
	''' </summary>
	Public Class UsersBlocked
		Inherits UserUsersList
		Private getBlockingUsers As Boolean

		Public Sub New(ByVal usr As User)
			MyBase.New(usr)
			getBlockingUsers = False
		End Sub

		Public Sub New(ByVal userID As Integer)
			MyBase.New(userID)
			getBlockingUsers = False
		End Sub

		Public Sub New(ByVal usr As User, ByVal blockingUsers As Boolean)
			MyBase.New(usr)
			getBlockingUsers = blockingUsers
		End Sub

		Public Sub New(ByVal userID As Integer, ByVal blockingUsers As Boolean)
			MyBase.New(userID)
			getBlockingUsers = blockingUsers
		End Sub

		' Checks if the user is blocked
		' by a specific other user
		Public Shared Function IsUserBlockedBy(ByVal userID As Integer, ByVal blockUserID As Integer) As Boolean
			Dim res As Boolean = False

			' Check first if we're blocked
			Dim conn As SqlConnection = DBManager.GetUnmanagedConnection()
			Dim trans As SqlTransaction = conn.BeginTransaction()

			Try
				res = IsUserBlockedBy(trans, userID, blockUserID)
				trans.Commit()
			Catch generatedExceptionName As Exception
				trans.Rollback()
				Throw
			Finally
				conn.Close()
			End Try

			Return res
		End Function

		' Checks if the user is blocked
		' by a specific other user
		Public Shared Function IsUserBlockedBy(ByVal trans As SqlTransaction, ByVal userID As Integer, ByVal blockUserID As Integer) As Boolean
			If trans = Nothing Then
				Return IsUserBlockedBy(userID, blockUserID)
			End If

			Dim res As Boolean = False

			Dim cmd As New SqlCommand("SELECT * FROM BlockedUsers WHERE UID=@UID AND BlockedUID=@BlockedUID")
			cmd.Connection = trans.Connection
			cmd.Transaction = trans

			cmd.Parameters.Add("@UID", blockUserID)
			cmd.Parameters.Add("@BlockedUID", userID)

			Dim dr As SqlDataReader = cmd.ExecuteReader()
			If dr.Read() Then
				res = True
			End If
			dr.Close()

			Return res
		End Function

		Protected Overloads Overrides Function LoadList(ByVal userID As Integer) As Hashtable
			Dim buList As New Hashtable()

			Dim conn As SqlConnection = DBManager.GetUnmanagedConnection()
			Try
				Dim cmd As SqlCommand = Nothing

				If Not getBlockingUsers Then
					cmd = New SqlCommand("SELECT * FROM BlockedUsers WHERE UID=@UID")
				Else
					cmd = New SqlCommand("SELECT * FROM BlockedUsers WHERE BlockedUID=@UID")
				End If

				cmd.Connection = conn
				cmd.Parameters.Add("@UID", userID)

				Dim dr As SqlDataReader = cmd.ExecuteReader()
				If Not getBlockingUsers Then
					While dr.Read()
						Dim usr As New BlockedUser(dr)
						buList.Add(usr.GetHashCode(), usr)
					End While
				Else
					Dim ID As Integer = -1
					Dim UID As Integer = -1

					While dr.Read()
						ID = dr.GetInt32(dr.GetOrdinal("ID"))
						UID = dr.GetInt32(dr.GetOrdinal("UID"))
						Dim usr As User = Users.GetUserByID(UID)

						buList.Add(usr.GetHashCode(), usr)
					End While
				End If
				dr.Close()
			Finally
				conn.Close()
			End Try

			Return buList
		End Function

		Protected Overloads Overrides Sub ClearList()
			Dim conn As SqlConnection = DBManager.GetUnmanagedConnection()
			Try
				Dim cmd As New SqlCommand("DELETE FROM BlockedUsers WHERE UID=@UID")
				cmd.Connection = conn
				cmd.Parameters.Add("@UID", userID)

				cmd.ExecuteNonQuery()
			Finally
				conn.Close()
			End Try
		End Sub

		Protected Overloads Overrides Sub DeleteItem(ByVal bUID As Integer)
			Dim bu As New BlockedUser(userID, bUID)
			bu.Delete()
		End Sub

		Protected Overloads Overrides Sub DeleteItem(ByVal item As Object)
			Dim bu As BlockedUser = DirectCast(item, BlockedUser)
			bu.Delete()
		End Sub

		Protected Overloads Overrides Function AddItem(ByVal item As Object) As Boolean
			Dim bu As BlockedUser = DirectCast(item, BlockedUser)
			Debug.Assert(bu.UserID = Me.userID)
			bu.Save()

			Return True
		End Function
	End Class
End Namespace






